//
//  Lazer.m
//  ProjectL
//
//  Created by Nguyen Tan Loc on 8/16/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "Lazer.h"
#import "Utility.h"

@implementation Lazer

-(id) initWithList:(NSArray *)listNode
{
    if (self = [super init])
    {
        [self initObject:listNode];
    }
    
    return self;
}

-(void) initObject:(NSArray *)listLNode
{
    mListNode = [[NSMutableArray alloc] initWithArray:listLNode];
    mListLine = [[NSMutableArray alloc] init];

    for (int i = 0; i < listLNode.count - 1; i++)
    {
        LNode *node = [mListNode objectAtIndex:i];
        LNode *nextNode = [mListNode objectAtIndex:(i + 1)];
        
        node._nextNode = nextNode;
    }
    
    for (int i = 0; i < mListNode.count - 1; i++)
    {
        LNode *node = [mListNode objectAtIndex:i];
        LLine *line = [[LLine alloc] initWithNode:node];
        
        [mListLine addObject:line];
        [line release];
    }
    
//    for (int i = 0; i < mListLine.count; i++)
//    {
//        LLine *line = [mListLine objectAtIndex:i];
//        [self addChild:line];
//    }
//    
//    for (int i = 0; i < mListNode.count; i++)
//    {
//        LNode *node = [mListNode objectAtIndex:i];
//        [self addChild:node];
//
//    }
}

-(void) addToBatch:(CCSpriteBatchNode *)batch
{
    for (int i = 0; i < mListLine.count; i++)
    {
        LLine *line = [mListLine objectAtIndex:i];
        [batch addChild:line z:Z_ORDER_LAZER_LINE];
    }
    
    for (int i = 0; i < mListNode.count; i++)
    {
        LNode *node = [mListNode objectAtIndex:i];
        [batch addChild:node z:Z_ORDER_LAZER_NODE];
    }
}

-(void) addActions:(NSArray *)actions
{
}

-(void) startAction
{
    CCLOG(@"lazer start action");
    if (mActionDelay != nil)
    {
        [self runAction:mActionDelay];
    }
    
    for (int i = 0; i < mListNode.count; i++)
    {
        LNode *node = [mListNode objectAtIndex:i];
        [node startAction];
    }
//    [self.scheduler setTimeScale:0.3];
}

-(void) stopAction
{
    [self stopAllActions];
    [mActionDelay release];
    [mActionOnOff release];
    
    for (int i = 0; i < mListNode.count; i++)
    {
        LNode *node = [mListNode objectAtIndex:i];
        [node stopAction];
    }
}

-(void) setTimeDelay:(float)timeDelay timeOn:(float)timeOn timeOff:(float)timeOff
{
    if (timeOff <= 0) // does not need to run action
        return;
    
    [self offWithoutSound];
    CCDelayTime *aDelay = [CCDelayTime actionWithDuration:timeDelay];
    CCCallFunc *funcDelay = [CCCallFunc actionWithTarget:self selector:@selector(endDelay)];
    CCSequence *sequenceDelay = [CCSequence actionOne:aDelay two:funcDelay];
    mActionDelay = [sequenceDelay retain];
    
    CCDelayTime *aOn = [CCDelayTime actionWithDuration:timeOn];
    CCDelayTime *aOff = [CCDelayTime actionWithDuration:timeOff];
    CCCallFunc *funcOn = [CCCallFunc actionWithTarget:self selector:@selector(on)];
    CCCallFunc *funcOff = [CCCallFunc actionWithTarget:self selector:@selector(off)];
    
    CCSequence *sequenceOnOff = [CCSequence actions:funcOn, aOn, funcOff, aOff, nil];
    CCRepeatForever *repeat = [CCRepeatForever actionWithAction:sequenceOnOff];
    mActionOnOff = [repeat retain];
}

-(void) on
{
    for (int i = 0; i < mListLine.count; i++)
    {
        LLine *line = [mListLine objectAtIndex:i];
        [line on];
    }
    
    if (self.position.y < [CCDirector sharedDirector].winSize.height + 200)
    {
        [[Utility getInstance] playEffect:SOUND_LAZER];
    }
}

-(void) off
{
    for (int i = 0; i < mListLine.count; i++)
    {
        LLine *line = [mListLine objectAtIndex:i];
        [line off];
    }
    
    if (self.position.y < [CCDirector sharedDirector].winSize.height + 200)
    {
        [[Utility getInstance] playEffect:SOUND_LAZER];
    }
}

-(void) offWithoutSound
{
    for (int i = 0; i < mListLine.count; i++)
    {
        LLine *line = [mListLine objectAtIndex:i];
        [line off];
    }
}

-(void) endDelay
{
    [self runAction:mActionOnOff];
    CCLOG(@"end delay");
}

-(void) dealloc
{
    [mListNode removeAllObjects];
    [mListNode release];
    mListNode = nil;
    
    [mListLine removeAllObjects];
    [mListLine release];
    mListLine = nil;
    
    [super dealloc];
    CCLOG(@"Lazer dealloc!");
}

@end
